﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Threading;

namespace DWF.Studio.Custom
{
    static class WPFExtensionMethods
    {
        public static Point GetAbsolutePosition(this Window window)
        {
            if (window.WindowState != WindowState.Maximized)
                return new Point(window.Left, window.Top);

            Int32Rect r;
            bool multimonSupported = WinApiInterop.GetSystemMetrics(WinApiInterop.SM_CMONITORS) != 0;
            if (!multimonSupported)
            {
                WinApiInterop.RECT rc = new WinApiInterop.RECT();
                WinApiInterop.SystemParametersInfo(48, 0, ref rc, 0);
                r = new Int32Rect(rc.left, rc.top, rc.width, rc.height);
            }
            else
            {
                WindowInteropHelper helper = new WindowInteropHelper(window);
                IntPtr hmonitor = WinApiInterop.MonitorFromWindow(new HandleRef((object)null, helper.EnsureHandle()), 2);
                WinApiInterop.MONITORINFOEX info = new WinApiInterop.MONITORINFOEX();
                WinApiInterop.GetMonitorInfo(new HandleRef((object)null, hmonitor), info);
                r = new Int32Rect(info.rcWork.left, info.rcWork.top, info.rcWork.width, info.rcWork.height);
            }
            return new Point(r.X, r.Y);
        }
        /// <summary>
        /// 找到节点的子类节点  修改搜索框覆盖层用的  20200513 Carl
        /// </summary>
        /// <typeparam name="childItem"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is childItem)
                    return (childItem)child;
                else
                {
                    childItem childOfChild = FindVisualChild<childItem>(child);
                    if (childOfChild != null)
                        return childOfChild;
                }
            }
            return null;
        }
        public static DependencyObject VisualUpwardSearch<T>(DependencyObject source)
        {
            while (source != null && source.GetType() != typeof(T))
                source = VisualTreeHelper.GetParent(source);

            return source;
        }
        public static bool existFromUrl(String url)
        {
            System.Uri uri = null;
            try
            {
                uri = new Uri(url);
            }
            catch { return false; }
            bool isExist = false;
            System.Net.HttpWebRequest httpWebRequest = System.Net.HttpWebRequest.Create(uri) as System.Net.HttpWebRequest;
            httpWebRequest.Timeout = 1000;
            httpWebRequest.Method = "HEAD";
            try
            {
                var requestResp = httpWebRequest.GetResponse() as System.Net.HttpWebResponse;
                if (requestResp == null)
                    return isExist;
                System.Net.HttpWebResponse httpWebResponse = requestResp;
                if (httpWebResponse.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    isExist = true;
                }
            }
            catch (System.Net.WebException x)
            {
                if (x.Status == WebExceptionStatus.Timeout || x.Status == WebExceptionStatus.ConnectFailure || x.Status == WebExceptionStatus.RequestCanceled)
                    return false;
                try
                {
                    isExist = ((x.Response as System.Net.HttpWebResponse).StatusCode != System.Net.HttpStatusCode.NotFound);
                }
                catch { isExist = (x.Status == System.Net.WebExceptionStatus.Success); }
            }
            return isExist;
        }
        [DllImport("user32.dll")] public static extern int MessageBoxTimeoutA(IntPtr hWnd, string msg, string Caps, int type, int Id, int time);
        public static void ShowDelayMessage(string title, string message)
        {
            MessageBoxTimeoutA((IntPtr)0, message, title, 0, 0, 1000); // 直接调用 3秒后自动关闭
        }
        public static void Refresh(this UIElement uiElement)
        {
            uiElement?.Dispatcher.InvokeAsync(() => { Thread.Sleep(50); }, DispatcherPriority.Render);
        }
    }
}
